class SelectSearchController < ApplicationController
  filter_access_to :all

  # GET /select_search/new
  def new
    @artist_search = Artist.search(params[:search])
   
    if !params[:search].nil? && !params[:search][:name_like].blank?
      @artists = @artist_search.all
    end
  end

  # POST /select_search/
  def create
    #Checks if a new artist needs to be created
    # otherwise retrieves the selected artist
    if params[:name] != nil && params[:artist_id].nil?
      artist = Artist.new(:name => params[:name])
      artist.save
    else
      artist = Artist.find(params[:artist_id])
    end

    respond_to do |format|
     
      #make sure an artist was returned
      if !artist.nil?
        format.html { redirect_back_with_artist(artist.id) }
      else
        flash[:error] = 'Something went wrong. Sorry.'
        format.html { redirect_to root_url }
      end
    end
  end

end
